// pages/appiointChoose/timeAndCardChoose.js
const api = require("../../utils/api")
const util = require("../../utils/util")
const cache = require("../../utils/cache")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0, //当前页面
    scrollLeft: 0,
    fold: false, //号源是否展开
    sourceList: [], //号源列表
    schedulingList: [], //排班列表
    curScheduling: {}, //当前排班
    curApe: '', //当前 上午 下午 晚上标识
    dateList: [], //时间列表
    doctor: '', //医生信息
    nowDate: '', //当前时间
    sourceId: '', //选中的号源id
  },
  handleRadioChange(e) {
    this.data.sourceId = e.detail.value
  },
  handleAppioint(e) { //预约方法
    if (this.data.dateList.length > 0) {
      if (this.data.sourceId == '' || this.data.sourceId == undefined || this.data.sourceId == null) {
        wx.showToast({
          title: '请选择就诊时间',
        })
        return;
      }
      let appiointDate = this.data.dateList[this.data.TabCur]
      let scheduling = this.data.schedulingList.find(m => m.date == appiointDate)
      if (scheduling == undefined || scheduling == null) {
        return;
      }
      let curPt = cache.currentPatient();
      if (curPt == undefined || curPt == null) {
        wx.showToast({
          title: '请选择就诊人',
        })
        return;
      }

      var param = {
        sourceId: this.data.sourceId,
        ioNo: curPt.ioNo,
        target: '100014',
        child: ''
      }
      if (e.currentTarget.dataset.target == 'oth') { //预约
        api.appointment(param).then(res => {
          if (res) {
            wx.showToast({
              title: '预约成功!',
            })
            setTimeout(() => {
              wx.navigateTo({
                url: '/pages/register/registerAndAppiointHistory?id=1',
              })
            }, 1000);
          }
        })
      } else if (e.currentTarget.dataset.target == 'cur') { //当天挂号
        api.register(param).then(res => {
          if (res) {
            wx.showToast({
              title: '挂号成功!',
            })
            setTimeout(() => {
              wx.navigateTo({
                url: '/pages/register/registerAndAppiointHistory?id=0',
              })
            }, 1000);
          }
        })
      }
    }
  },
  tabSelect(e) {
    if (this.data.dateList != undefined && this.data.dateList != null && this.data.dateList.length > 0) {
      //获取当前排班
      let date = this.data.dateList[e.currentTarget.dataset.id]
      this.data.curScheduling = this.data.schedulingList.find(m => m.date == date)
      let ape = ''
      if (this.data.nowDate == date) {
        ape = this.data.curApe
      }
      this.handleLoadSourceList({
        scheid: this.data.curScheduling.scheId,
        ape: ape
      })

      this.setData({
        TabCur: e.currentTarget.dataset.id,
        scrollLeft: (e.currentTarget.dataset.id - 1) * 60,
        curScheduling: this.data.curScheduling,
        nowDate: this.data.nowDate
      })
    }
  },
  handleFold() {
    this.setData({
      fold: !this.data.fold
    })
  },
  handleDoctorDetail(e) {
    let that = this
    wx.navigateTo({
      url: '/pages/doctorDetail/doctorDetail',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('doctorDetail', {
          data: that.data.doctor
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    //当前时间
    that.data.nowDate = util.timestampToLocalFormat(Date.parse(new Date()))

    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('data', function (data) {
      that.data.dateList = data.dateList
      that.data.schedulingList = data.schedulingList
    });
    this.data.curApe = options.ape
    this.handleLoadSourceList(options);
    //获取当前排班
    that.data.curScheduling = that.data.schedulingList.find(m => m.scheId == options.scheid)
    let index = that.data.schedulingList.findIndex(m => m.scheId == options.scheid)
    this.setData({
      curScheduling: that.data.curScheduling,
      nowDate: that.data.nowDate,
      TabCur: index
    })
  },
  handleLoadSourceList(options) {
    let that = this
    api.getRegistrationSource({
      scheId: options.scheid,
      ape: options.ape
    }).then(res => {
      that.data.sourceList = res
      // 获取当前排班
      let scheduling = that.data.schedulingList.find(m => m.scheId == options.scheid)
      that.data.doctor = {
        code: scheduling.drCode,
        name: scheduling.drName,
        levelName: scheduling.level,
        introduce: scheduling.introduce,
        deptName: scheduling.deptName,
        url: scheduling.image
      }
      that.setData({
        sourceList: that.data.sourceList,
        dateList: that.data.dateList,
        schedulingList: that.data.schedulingList,
        doctor: that.data.doctor
      })
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this.selectComponent("#card")
    that.callbackFunc(() => that.loadData())
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})